﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyProj.MVCProj3.Model;

namespace MyProj.MVCProj3.UI.Main.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        BLL.UserInfoService userInfoService = new BLL.UserInfoService();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 加载用户权限菜单项
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadMenu()
        {

            //查询出当前用户信息
            var user = userInfoService.LoadEntities(u => u.ID == 3).FirstOrDefault();
            short menuType = (short)MyProj.MVCProj3.Model.Enum.ActionGroupTypeEnum.MenuGroup;
            //通过查询当前用户的角色——将所有的菜单组（权限组）查询出来
            var groupData = from r in user.Role
                            from g in r.ActionGroup
                            where g.GroupType == menuType
                            select g;

            //组合前台需要的菜单项  和菜单组的组合数据
            var data = from g in groupData
                       select new
                       {
                           g.ID,
                           g.GroupName,
                           g.Sort,
                           MenuItems = from a in g.ActionInfo//把当前权限组里面的权限查询出来
                                       where a.IsMenu == true
                                       select new { a.ID, a.ActionInfoName, a.Url, a.HttpMethod }
                       };
            //返回Json数据
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
